package com.yichenyang.ordersystem.controller;

import com.yichenyang.ordersystem.entity.*;
import com.yichenyang.ordersystem.service.OrderdetailService;
import com.yichenyang.ordersystem.service.OrdermasterService;
import com.yichenyang.ordersystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrdermasterService ordermasterService;

    @Autowired
    OrderdetailService orderdetailService;

    @Autowired
    ProductService productService;

    private int pageSize = 10;
    private int pageCount = 0;
    private int pageTotal;


    @RequestMapping("/list")
    public String memberList(HttpSession session) {
        session.setAttribute("page",1);
        int customersAll = ordermasterService.queryAll();
        session.setAttribute("orderNum",customersAll);
        session.setAttribute("orderPages",getPages());

        List<Ordermaster> orders = ordermasterService.queryAllByLimit(0, pageSize);
        session.setAttribute("orders",orders);
        return "pages/order/list::orderList";
    }

    @PostMapping("/add")
    @ResponseBody
    public Integer memberAdd(@RequestBody Ordermaster ordermaster) {
        ordermaster = ordermasterService.insert(ordermaster);
        return ordermaster.getOrderId();
    }

    @PostMapping("/addProducts")
    @ResponseBody
    public void memberList(@RequestBody List<Orderdetail> orderdetailList) {
        for (Orderdetail orderdetail : orderdetailList) {
            orderdetailService.insert(orderdetail);
        }
    }

    //换页
    @PostMapping("/page/{page}")
    public String changePage(@PathVariable int page,HttpSession session) {
        pageCount = (page-1)*10;
        List<Ordermaster> orders = ordermasterService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("orders",orders);
        return "pages/order/list::orderList";
    }

    @PostMapping("/find")
    public String orderFind(@RequestBody Map<String,String> map, HttpSession session) {
        if (session.getAttribute("orders")!=null) {
            List<Ordermaster> orders = ordermasterService.queryByConditions(map.get("payMethod"),map.get("sendMethod"),map.get("orderId"),map.get("start"),map.get("end"));
            session.setAttribute("orders",orders);
        }
        return "pages/order/list::orderList";
    }

    @GetMapping("/find/{id}")
    @ResponseBody
    public String orderFindById(@PathVariable int id) {
        Ordermaster ordermaster = ordermasterService.queryById(id);
        return ordermaster.getDescription();
    }

    @GetMapping("/findProduct/{id}")
    @ResponseBody
    public List<OrderProduct> orderFindProductById(@PathVariable int id) {
        List<OrderProduct> orderProducts = new CopyOnWriteArrayList<>();
        List<Orderdetail> orderdetails = orderdetailService.queryById(id);
        for (Orderdetail orderdetail : orderdetails) {
            OrderProduct orderProduct = new OrderProduct();
            orderProduct.setPid(orderdetail.getProductId());
            orderProduct.setPrice(orderdetail.getPrice());
            orderProduct.setQty(orderdetail.getQty());
            orderProduct.setSum(orderdetail.getSum());
            Product product = productService.queryById(orderdetail.getProductId());
            orderProduct.setName(product.getProductName());
            orderProducts.add(orderProduct);
        }
        return orderProducts;
    }

    //删除信息
    @GetMapping("/del/{id}")
    public String del(@PathVariable int id, HttpSession session) {
        orderdetailService.deleteById(id);
        ordermasterService.deleteById(id);
        List<Ordermaster> orders = ordermasterService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("orders",orders);
        return "pages/order/list::orderList";
    }

    //更改信息
    @PostMapping("/update/{id}")
    @ResponseBody
    public void update(@PathVariable int id,@RequestBody Ordermaster ordermaster,HttpSession session) {
        session.setAttribute("orderId",id);
        ordermaster.setOrderId(id);
        ordermasterService.update(ordermaster);
    }

    //修改订单明细表
    @PostMapping("/updateProducts")
    @ResponseBody
    public void updateProducts(@RequestBody List<Orderdetail> orderdetailList,HttpSession session) {
        orderdetailService.deleteById((Integer) session.getAttribute("orderId"));
        session.removeAttribute("orderId");
        for (Orderdetail orderdetail : orderdetailList) {
            orderdetailService.insert(orderdetail);
        }
    }

    public List<Integer> getPages() {
        int ordersAll = ordermasterService.queryAll();
        pageTotal = ordersAll / pageSize + (ordersAll % pageSize == 0 ? 0:1);
        List<Integer> pages = new CopyOnWriteArrayList<>();
        for (int i = 0; i < pageTotal; i++) {
            pages.add(i+1);
        }
        return pages;
    }

}
